<template>
  <div class="registered-container">
    <el-form :model="form" :rules="rules"
             status-icon
             ref="form"
             label-position="left"
             label-width="0px"
             class="demo-ruleForm registered-page">
      <h3 class="title">申请账号</h3>
      <el-form-item prop="username">
        <el-input type="text"
                  v-model="form.username"
                  auto-complete="off"
                  placeholder="用户名"
        ></el-input>
      </el-form-item>
      <el-form-item prop="phone">
        <el-input type="text"
                  v-model="form.phone"
                  auto-complete="off"
                  placeholder="手机号"
        ></el-input>
      </el-form-item>
      <el-form-item prop="email">
        <el-input type="text"
                  v-model="form.email"
                  auto-complete="off"
                  placeholder="邮箱"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password"
                  v-model="form.password"
                  auto-complete="off"
                  placeholder="密码"
        ></el-input>
      </el-form-item>
      <el-form-item prop="repassword">
        <el-input type="password"
                  v-model="form.repassword"
                  auto-complete="off"
                  placeholder="确认密码"
        ></el-input>
      </el-form-item>
      <el-form-item prop="roleId">
        <el-select v-model="form.roleId" placeholder="请选择用户角色">
          <el-option
              v-for="item in formDefault.role"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button type="primary" style="width:50%;" @click="handleSubmit">确认申请</el-button>
        <el-button type="danger" style="width:45%;" @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {post, asyncGet, get} from "@/plugins/axios"
import { Message } from "element-ui"

export default {
  name: 'registered',
  created() {
    this.getRoles()
  },
  data(){
    const module = '/user'
    const checkPassword = (rule, value, callback) => {
      if (this.form.password === this.form.repassword) {
        callback()
      } else {
        callback(new Error('两次输入密码不相同'))
      }
    }
    const check = async (rule, value, callback) => {
      const response = await asyncGet(module + '/check', {value: value})
      if (response.data.data) {
        callback()
      } else {
        callback(new Error('已存在相同数据'))
      }
    }
    return {
      uri: {
        registered: module + '/saveOrUpdate',
        getRoles: '/role/registerRole'
      },
      form: {
        username: '',
        phone: '',
        email: '',
        password: '',
        repassword: '',
        roleId: ''
      },
      formDefault: {
        role: []
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {max: 30, message: '用户名不能超过30个字符', trigger: 'blur'},
          {validator: check, trigger: 'blur'}
        ],
        phone: [
          {required: true, message: '请输入手机号', trigger: 'blur'},
          {pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/, message: '请输入正确的手机号格式', trigger: 'blur'},
          {validator: check, trigger: 'blur'}
        ],
        email: [
          {required: true, message: '请输入邮箱', trigger: 'blur'},
          {type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur'},
          {validator: check, trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ],
        repassword: [
          {required: true, message: '请确认密码', trigger: 'blur'},
          {validator: checkPassword, trigger: 'blur'}
        ],
        roleId: [
          {required: true, message: '请选择用户角色', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    handleSubmit(){
      this.$refs.form.validate((valid) => {
        if(valid){
          post(this.uri.registered, this.form, response => {
            if (response.code === 200) {
              Message.success('账号已成功注册，请耐心等待管理员审批')
              this.$router.push({path: '/login'})
            }
          })
        }else{
          return false
        }
      })
    },
    cancel() {
      this.$confirm('是否返回登录页?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$router.back();
      })
    },
    getRoles() {
      get(this.uri.getRoles, {status: 1}, response => {
        const roles = response.data;
        for (const item in roles) {
          this.formDefault.role.push({
            label: roles[item].roleName,
            value: roles[item].id
          })
        }
      })
    }
  }
};
</script>

<style scoped lang="less">
.registered-container {
  width: 100%;
  height: 100%;
  .registered-page {
    -webkit-border-radius: 5px;
    border-radius: 5px;
    margin: 180px auto;
    width: 350px;
    padding: 35px 35px 15px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;

    label.el-checkbox.rememberme {
      margin: 0px 0px 15px;
      text-align: left;
    }
  }
}
</style>